﻿using System;
using System.Windows.Forms;
using LCL.LData;
using LCL.Config;

namespace LCL.DbSqlMonitor
{
    public partial class SqlMonitor : Form
    {
        private DbConfigInfo conf = new DbConfigInfo();
        private ISQLMonitor iSQLMonitor = null;
        private System.Windows.Forms.Timer _timer;
        private long _timerCount = 0;
        public SqlMonitor()
        {
            InitializeComponent();
        }
        private void DbSqlMonitor_Load(object sender, EventArgs e)
        {
            InitSeting();
            this.labMsg.Text = "";
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            InitEnabled(false);
            InitConfig();
            if (iSQLMonitor == null)
            {
                MessageBox.Show("配置文件不正确，无法加载对应的数据库的适配器！");
                InitEnabled(true);
                return;
            }
            if (iSQLMonitor.DbLinkTest())
            {
                this._timerCount = 1;
                this._timer = new System.Windows.Forms.Timer();
                this._timer.Enabled = true;
                this._timer.Interval = Convert.ToInt32(100) * 1000;
                this._timer.Tick += new EventHandler(this._timer_Tick);
                InitEnabled(false);
                InitTable();
            }
            else
            {
                InitEnabled(false);
            }
        }
        private void _timer_Tick(object sender, EventArgs e)
        {
            try
            {
                InitTable();
                this._timerCount++;
                this.labMsg.Text = "轮询" + this._timerCount.ToString() + "次";
            }
            catch (Exception ex)
            {
                Logger.Error("SQLMonitor:", ex);
            }
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            this.labMsg.Text = "";
            this.btnStart.Enabled = true;
            this.btnStop.Enabled = false;
            this._timer = null;
            InitEnabled(true);
            if (iSQLMonitor != null)
            {
                iSQLMonitor.Clear();
                return;
            }
        }
        private void comboBoxDBType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.comboBoxServer.Items.Clear();
            this.txtUser.Items.Clear();
            this.comboBoxServer.Text = "";
            this.txtUser.Text = "";
            this.txtPass.Text = "";
            conf.DbType = this.comboBoxDBType.Text.ToEnum<LDbType>();
            switch (conf.DbType)
            {
                case LDbType.MySQL:
                    this.comboBoxServer.Items.Add("localhost");
                    this.comboBoxServer.Items.Add("127.0.0.1");
                    this.txtUser.Items.Add("root");
                    this.comboBoxServer.Text = "localhost";
                    this.txtUser.Text = "root";
                    this.txtPass.Text = "123456";
                    break;
                case LDbType.SQLServerCe:
                case LDbType.SQLServer:
                    this.comboBoxServer.Items.Add("localhost");
                    this.comboBoxServer.Items.Add("127.0.0.1");
                    this.txtUser.Items.Add("sa");
                    this.comboBoxServer.Text = "localhost";
                    this.comboBoxServer.Text = "localhost";
                    this.txtUser.Text = "sa";
                    this.txtPass.Text = "123456";
                    break;
                case LDbType.SQLite:
                    break;
                case LDbType.Oracle:
                    this.comboBoxServer.Items.Clear();
                    this.comboBoxServer.Items.Add("emeeting_test");
                    this.comboBoxServer.Items.Add("career_new");
                    this.comboBoxServer.Items.Add("VISATEST");
                    this.comboBoxServer.Items.Add("TEST159");
                    this.comboBoxServer.Items.Add("HOL_UAT");
                    this.txtUser.Items.Clear();
                    this.txtUser.Items.Add("emeetingjc");
                    this.txtUser.Items.Add("emeeting");
                    this.txtUser.Items.Add("career");
                    this.txtUser.Items.Add("visaxt");
                    this.txtUser.Items.Add("asuser");
                    this.txtUser.Items.Add("hronline");

                    this.comboBoxServer.Text = "emeeting_test";
                    this.txtUser.Text = "emeetingjc";
                    this.txtPass.Text = "emeetingjc";
                    break;
                case LDbType.ODBC:
                    break;
                case LDbType.OleDb:
                    break;
                case LDbType.MongoDB:
                    break;
                default:
                    break;
            }
        }
        private void txtUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = this.txtUser.SelectedItem.ToString().Trim();
            switch (conf.DbType)
            {
                case LDbType.MySQL:
                    break;
                case LDbType.SQLServer:
                    break;
                case LDbType.SQLServerCe:
                    break;
                case LDbType.SQLite:
                    break;
                case LDbType.Oracle:
                    #region MyRegion
                    if (text.Equals("emeetingjc"))
                    {
                        this.txtPass.Text = "emeetingjc";
                    }
                    else if (text.Equals("emeeting"))
                    {
                        this.txtPass.Text = "emeeting_2012";
                    }
                    else if (text.Equals("career"))
                    {
                        this.txtPass.Text = "CAREER";
                    }
                    else if (text.Equals("visaxt"))
                    {
                        this.txtPass.Text = "visaxt";
                    }
                    else if (text.Equals("asuser"))
                    {
                        this.txtPass.Text = "asuser";
                    }
                    else if (text.Equals("hronline"))
                    {
                        this.txtPass.Text = "Ora123456";
                    }
                    #endregion
                    break;
                case LDbType.ODBC:
                    break;
                case LDbType.OleDb:
                    break;
                case LDbType.MongoDB:
                    break;
                default:
                    break;
            }
        }
        private void InitConfig()
        {
            conf = DbConfig.GetConfig();
            if (conf == null) conf = new DbConfigInfo();
            conf.DbHost = this.comboBoxServer.Text.Trim();
            conf.DbType = this.comboBoxDBType.Text.ToEnum<LDbType>();
            conf.DbUser = this.txtUser.Text.Trim();
            conf.DbPwd = this.txtPass.Text.Trim();
            iSQLMonitor = DBMonitorFactory.CreateDBMonitor(conf);
            DbConfig.SaveConfig(conf);
        }
        private void InitTable()
        {
            var table = iSQLMonitor.GetSqlALL();
            if (table != null)
            {
                this.dataGridView1.DataSource = table;
                string file = Utils.GetMapPath("Logs\\SQLMonitor_" + DateTime.Now.ToString("yyyyMMdd") + "_" + this._timerCount + ".xml");
                table.WriteXml(file);
            }
        }
        private void InitEnabled(bool flg)
        {
            this.comboBoxServer.Enabled = flg;
            this.comboBoxDBType.Enabled = flg;
            this.txtUser.Enabled = flg;
            this.txtPass.Enabled = flg;

            if (flg)
            {
                btnStart.Enabled = true;
                btnStop.Enabled = false;
            }
            else
            {
                btnStart.Enabled = false;
                btnStop.Enabled = true;
            }
        }
        private void InitSeting()
        {
            this.comboBoxDBType.DataSource = System.Enum.GetNames(typeof(LDbType));
            this.comboBoxDBType.SelectedIndex = 4;//默认选中Oracle
            conf = DbConfig.GetConfig();
            if (conf != null)
            {
                this.comboBoxServer.Text = conf.DbHost;
                this.comboBoxDBType.SelectedItem = conf.DbType;
                this.comboBoxDBType.Text = conf.DbType.ToString();
                this.txtUser.Text = conf.DbUser;
                this.txtPass.Text = conf.DbPwd;
            }
        }
    }
}
